Skip to content

Inheritance

Creating Superclasses and Subclasses

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Create an inheritance relationship from a subclass to the superclass.

ESSENTIAL KNOWLEDGE

  • A class hierarchy can be developed by putting common attributes and behaviors of related classes into a single class called a superclass.

  • Classes that extend a superclass, called subclasses, can draw upon the existing attributes and behaviors of the superclass without repeating these in the code.

  • Extending a subclass from a superclass creates an “is-a” relationship from the subclass to the superclass.

  • The keyword extends is used to establish an inheritance relationship between a subclass and a superclass. A class can extend only one superclass.

Writing Constructors for Subclasses

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Create an inheritance relationship from a subclass to the superclass.

ESSENTIAL KNOWLEDGE

  • Constructors are not inherited.

  • The superclass constructor can be called from the first line of a subclass constructor by using the keyword super and passing appropriate parameters.

  • The actual parameters passed in the call to the superclass constructor provide values that the constructor can use to initialize the object’s instance variables.

  • When a subclass’s constructor does not explicitly call a superclass’s constructor using super, Java inserts a call to the superclass’s no-argument constructor.

  • Regardless of whether the superclass constructor is called implicitly or explicitly, the process of calling superclass constructors continues until the Object constructor is called. At this point, all of the constructors within the hierarchy execute beginning with the Object constructor.

Overriding Methods

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Create an inheritance relationship from a subclass to the superclass.

ESSENTIAL KNOWLEDGE

  • Method overriding occurs when a public method in a subclass has the same method signature as a public method in the superclass.

  • Any method that is called must be defined within its own class or its superclass.

  • A subclass is usually designed to have modified (overridden) or additional methods or instance variables.

  • A subclass will inherit all public methods from the superclass; these methods remain public in the subclass.

super Keyword

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Create an inheritance relationship from a subclass to the superclass.

ESSENTIAL KNOWLEDGE

  • The keyword super can be used to call a superclass’s constructors and methods.

  • The superclass method can be called in a subclass by using the keyword super with the method name and passing appropriate parameters.

Creating References Using Inheritance Hierarchies

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Define reference variables of a superclass to be assigned to an object of a subclass in the same hierarchy.

ESSENTIAL KNOWLEDGE

  • When a class S “is-a” class T, T is referred to as a superclass, and S is referred to as a subclass.

  • If S is a subclass of T, then assigning an object of type S to a reference of type T facilitates polymorphism.

  • If S is a subclass of T, then a reference of type T can be used to refer to an object of type T or S.

  • Declaring references of type T, when S is a subclass of T, is useful in the following declarations:

    • Formal method parameters
    • arrays — T[] var ArrayList<T> var

Polymorphism

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Call methods in an inheritance relationship.

ESSENTIAL KNOWLEDGE

  • Utilize the Object class through inheritance.

  • At compile time, methods in or inherited by the declared type determine the correctness of a non-static method call.

  • At run-time, the method in the actual object type is executed for a non-static method call.

Object Superclass

ENDURING UNDERSTANDING

LEARNING OBJECTIVE

Call Object class methods through inheritance.

ESSENTIAL KNOWLEDGE

  • The Object class is the superclass of all other classes in Java.

  • The Object class is part of the java.lang package

  • The following Object class methods and constructors—including what they do and when they are used—are part of the Java Quick Reference:

  • boolean equals(Object other)

  • String toString()

  • Subclasses of Object often override the equals and toString methods with class- specific implementations.